package com.lemon.smartsafety.ui.activity.safe.data;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.event.datacoll.ThreePersonEvent;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.common.KeyBoardUtils;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.utils.common.ValidateUtils;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Cooliean on 16/4/11.
 */
public class CompanyBasicThreePersonActivity extends SwipeBackActivity {

    public static final String kTxtLegalPerson          = "txtLegalPerson";
    public static final String kTxtLegalPersonCid       = "txtLegalPersonCid";
    public static final String kTxtLegalPersonPhone     = "txtLegalPersonPhone";
    public static final String kTxtFireManager          = "txtFireManager";
    public static final String kTxtFireManagerCid       = "txtFireManagerCid";
    public static final String kTxtFireManagerPhone     = "txtFireManagerPhone";
    public static final String kTxtFireResponsible      = "txtFireResponsible";
    public static final String kTxtFireResponsibleCid   = "txtFireResponsibleCid";
    public static final String kTxtFireResponsiblePhone = "txtFireResponsiblePhone";


    @Bind(R.id.txtLegalPerson)
    MaterialEditText txtLegalPerson;
    @Bind(R.id.txtLegalPersonCid)
    MaterialEditText txtLegalPersonCid;
    @Bind(R.id.txtLegalPersonPhone)
    MaterialEditText txtLegalPersonPhone;
    @Bind(R.id.txtFireManager)
    MaterialEditText txtFireManager;
    @Bind(R.id.txtFireManagerCid)
    MaterialEditText txtFireManagerCid;
    @Bind(R.id.txtFireManagerPhone)
    MaterialEditText txtFireManagerPhone;
    @Bind(R.id.txtFireResponsible)
    MaterialEditText txtFireResponsible;
    @Bind(R.id.txtFireResponsibleCid)
    MaterialEditText txtFireResponsibleCid;
    @Bind(R.id.txtFireResponsiblePhone)
    MaterialEditText txtFireResponsiblePhone;
    @Bind(R.id.btn_submit)
    Button           btnSubmit;

    private Map<String, String> mValueMap = new HashMap<>();


    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mValueMap = (Map<String, String>) getIntent().getSerializableExtra(BundleKey.THREE_PERSON_VALUE);
        }

    }

    private boolean isMapEmpty(Map map) {
        if (map == null || map.isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    protected void initTitle() {
        setBarTitle("三类人员信息");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(CompanyBasicThreePersonActivity.class);

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_company_basic_info_three_person;
    }

    @Override
    protected void initViews() {
        super.initViews();
        if (!isMapEmpty(mValueMap)) {
            txtLegalPerson.setText(StringUtils.nullValue(mValueMap.get(kTxtLegalPerson)));
            txtLegalPersonCid.setText(StringUtils.nullValue(mValueMap.get(kTxtLegalPersonCid)));
            txtLegalPersonPhone.setText(StringUtils.nullValue(mValueMap.get(kTxtLegalPersonPhone)));
            txtFireManager.setText(StringUtils.nullValue(mValueMap.get(kTxtFireManager)));
            txtFireManagerCid.setText(StringUtils.nullValue(mValueMap.get(kTxtFireManagerCid)));
            txtFireManagerPhone.setText(StringUtils.nullValue(mValueMap.get(kTxtFireManagerPhone)));
            txtFireResponsible.setText(StringUtils.nullValue(mValueMap.get(kTxtFireResponsible)));
            txtFireResponsibleCid.setText(StringUtils.nullValue(mValueMap.get(kTxtFireResponsibleCid)));
            txtFireResponsiblePhone.setText(StringUtils.nullValue(mValueMap.get(kTxtFireResponsiblePhone)));
        }
    }

    private boolean validateForm() {
        if (!TextUtils.isEmpty(txtLegalPersonCid.getText()
                                                .toString()
                                                .trim()) && !ValidateUtils.isCid(txtLegalPersonCid.getText()
                                                                                                  .toString()
                                                                                                  .trim())) {
            txtLegalPersonCid.setError("身份证号码不合法");
            mHUDUtil.showErrorTip("身份证号码不合法");
            return false;
        }


/*        if(!TextUtils.isEmpty(txtLegalPersonPhone.getText()
                .toString()
                .trim()) ){
            txtLegalPersonPhone.setError("您的法定负责人手机号码不合法");
            mHUDUtil.showErrorTip("您的法定负责人手机号码不合法");
            return false;
        }*/




        if (!TextUtils.isEmpty(txtFireManagerCid.getText()
                                                .toString()
                                                .trim()) && !ValidateUtils.isCid(txtFireManagerCid.getText()
                                                                                                  .toString()
                                                                                                  .trim())) {
            txtFireManagerCid.setError("身份证号码不合法");
            mHUDUtil.showErrorTip("身份证号码不合法");
            return false;
        }


/*
        if(!TextUtils.isEmpty(txtFireManagerPhone.getText()
                .toString()
                .trim()) ){
            txtFireManagerPhone.setError("您的消防安全管理人手机号码不合法");
            mHUDUtil.showErrorTip("您的消防安全管理人手机号码不合法");
            return false;
        }
*/



        if (!TextUtils.isEmpty(txtFireResponsibleCid.getText()
                                                    .toString()
                                                    .trim()) && !ValidateUtils.isCid(txtFireResponsibleCid.getText()
                                                                                                          .toString()
                                                                                                          .trim())) {
            txtFireResponsibleCid.setError("身份证号码不合法");
            mHUDUtil.showErrorTip("身份证号码不合法");
            return false;
        }



   /*     if(!TextUtils.isEmpty(txtFireResponsiblePhone.getText()
                .toString()
                .trim()) ){
            txtFireResponsiblePhone.setError("您的消防安全负责人手机号码不合法");
            mHUDUtil.showErrorTip("您的消防安全负责人手机号码不合法");
            return false;
        }*/


        return true;
    }


    @OnClick({R.id.btn_submit})
    public void onClick(View view) {
        KeyBoardUtils.closeSoftInput(this);


        if (!validateForm()) {
            return;
        }

        if (isMapEmpty(mValueMap)) {
            mValueMap = new HashMap<>();
        }

        mValueMap.put(kTxtLegalPerson, StringUtils.nullValue(txtLegalPerson.getText().toString().trim()));
        mValueMap.put(kTxtLegalPersonCid, StringUtils.nullValue(txtLegalPersonCid.getText().toString().trim()));
        mValueMap.put(kTxtLegalPersonPhone, StringUtils.nullValue(txtLegalPersonPhone.getText().toString().trim()));


        mValueMap.put(kTxtFireManager, StringUtils.nullValue(txtFireManager.getText().toString().trim()));
        mValueMap.put(kTxtFireManagerCid, StringUtils.nullValue(txtFireManagerCid.getText().toString().trim()));
        mValueMap.put(kTxtFireManagerPhone, StringUtils.nullValue(txtFireManagerPhone.getText().toString().trim()));


        mValueMap.put(kTxtFireResponsible, StringUtils.nullValue(txtFireResponsible.getText().toString().trim()));
        mValueMap.put(kTxtFireResponsibleCid, StringUtils.nullValue(txtFireResponsibleCid.getText().toString().trim()));
        mValueMap.put(kTxtFireResponsiblePhone, StringUtils.nullValue(txtFireResponsiblePhone.getText()
                                                                                             .toString()
                                                                                             .trim()));

        ThreePersonEvent event = new ThreePersonEvent();
        event.setTag(ThreePersonEvent.class.getName());
        event.setData(mValueMap);
        AppService.getBus().post(event);
        callFinish();
    }
}
